package com.me.controllers;

import com.me.annotation.Admin;
import com.me.annotation.Inject;
import com.me.annotation.Route;
import com.me.model.Goods;
import com.me.model.viewmodel.DetailGoodsViewModel;
import com.me.model.viewmodel.GoodsPageViewModel;
import com.me.model.viewmodel.GoodsViewModel;
import com.me.service.GoodsService;
import com.me.service.TypeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;

@Route("goods")
public class GoodsController extends Controller{
	@Inject private GoodsService gserv;
	@Inject private TypeService tserv;

	@Inject
	public GoodsController(HttpServletRequest req, HttpServletResponse resp) {
		super(req, resp);
	}

	@Override
	public String index(String arg) {
		return "/";
	}

	public String classify(String arg) {
		try {
			String type = getParam("type");
			int page = Integer.parseInt(getParam("page"));
			int size = Integer.parseInt(getParam("size"));
			int count = gserv.getByType(type,0,999).size();
			Collection<Goods> goods = gserv.getByType(type, page,size);
			GoodsPageViewModel viewModel = new GoodsPageViewModel(goods,type,tserv.getName(type),page,count,size);
			return view("goodslist",viewModel);
		} catch (Exception e) {
			return null;
		}

	}

	public String recommend(String arg) {
		try {
			String type = getParam("type");
			int page = Integer.parseInt(getParam("page"));
			int size = Integer.parseInt(getParam("size"));
			int count = gserv.getByRec(type,0,999).size();
			Collection<Goods> goods = gserv.getByRec(type,page,size);
			GoodsPageViewModel viewModel = new GoodsPageViewModel(goods,type,gserv.getName(type),page,count,size);
			return view("goodslist", viewModel);
		} catch (Exception e) {
			return null;
		}

	}
	public String search(String arg) {
		try {
			String key = getParam("key");
			int page = Integer.parseInt(getParam("page"));
			int size = Integer.parseInt(getParam("size"));
			int count = gserv.getByKey(key,0,999).size();
			Collection<Goods> goods = gserv.getByKey(key, page,size);
			GoodsPageViewModel viewModel = new GoodsPageViewModel(goods,key,key,page,count,size);
			return view("search",viewModel);
		} catch (Exception e) {
			return null;
		}

	}
	public String detail(String arg) {
		try{
			Goods goods = gserv.getById(arg);
			String typeName = tserv.getName(goods.getTypeId());
			DetailGoodsViewModel viewModel = new DetailGoodsViewModel(goods,typeName);
			return view("detail",viewModel);

		}catch (Exception e) {
			return null;
		}
	}

	@Admin
	public String goodsManage(String arg) {
		int page = Integer.parseInt(getParam("page"));
		int size = Integer.parseInt(getParam("size"));
		Collection<Goods> goods = gserv.getAllForPage(0, 18);
		int count = gserv.getAllForPage(0, 999).size();
		GoodsPageViewModel viewModel = new GoodsPageViewModel(goods,null,null,page,count,size);
		return view("admin/goods_list",viewModel);
	}

	@Admin
	public String edit(String arg) {
		Goods g = gserv.getById(arg);
		GoodsViewModel viewModel = new GoodsViewModel(g);
		return view("admin/goods_edit", viewModel);
	}

	@Admin
	public String update() {
		GoodsViewModel vm = (GoodsViewModel) Model;
		Goods g = gserv.getById(vm.getId());
		g.fromView(vm);
		gserv.update(g);
		return "./goods/goodsManage?page=0&size=18";
	}

	@Admin
	public String add(String arg) {
		return view("admin/goods_add", new GoodsViewModel());
	}

	@Admin
	public String add() {
		GoodsViewModel vm = (GoodsViewModel) Model;
		Goods g = new Goods();
		g.fromView(vm);
		gserv.add(g);
		return "./goods/detail/" + g.getId();
	}
}
